package com.ctyon.watchmanager.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ctyon.common.util.GsonUtil;
import com.ctyon.common.util.TSnackbarUtil;
import com.ctyon.netlibrary.net.revert.BaseResponseEntity;
import com.ctyon.netlibrary.use.BaseObserver;
import com.ctyon.watchmanager.BasicApp;
import com.ctyon.watchmanager.R;
import com.ctyon.watchmanager.databinding.FragmentSmsBinding;
import com.ctyon.watchmanager.model.net.entity.SmsNotification;
import com.ctyon.watchmanager.model.net.service.NetService;
import com.ctyon.watchmanager.present.SmsPresenter;
import com.ctyon.watchmanager.ui.BaseFragment;
import com.orhanobut.logger.Logger;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * CreateDate：19-6-25 on 上午9:14
 * Describe:
 * Coder: lee
 */
public class SmsFragment extends BaseFragment {

    public static final String TAG = "SmsFragment";

    private FragmentSmsBinding mBinding;


    public static SmsFragment newInstance() {
        final SmsFragment fragment = new SmsFragment();
        final Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_sms, container, false);

        mBinding.setSmsPresenter(smsPresenter);

        mBinding.smsBattery.setOnClickListener((v) -> mBinding.smsBattery.toggle());

        mBinding.smsSos.setOnClickListener((v) -> mBinding.smsSos.toggle());

        return mBinding.getRoot();

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getStates();
    }

    private final SmsPresenter smsPresenter = () -> {
        String mId = String.valueOf(BasicApp.getInstance().getManager().getManagerId());
        String wId = BasicApp.getInstance().getWatch().getMeid();
        SmsNotification.Data smsNotification1 = new SmsNotification.Data();
        smsNotification1.setManager_id(mId);
        smsNotification1.setWatch_id(wId);
        smsNotification1.setType(1);
        smsNotification1.setStatus(mBinding.smsSos.isChecked() ? 1 : 0);
        SmsNotification.Data smsNotification2 = new SmsNotification.Data();
        smsNotification2.setManager_id(mId);
        smsNotification2.setWatch_id(wId);
        smsNotification2.setType(2);
        smsNotification2.setStatus(mBinding.smsBattery.isChecked() ? 1 : 0);
        String content1 = GsonUtil.getGsonInstance().toJson(smsNotification1);
        String content2 = GsonUtil.getGsonInstance().toJson(smsNotification2);
        String content = "[" + content1 + "," + content2 + "]";
        Logger.t(LTAG).i("smsNotification : content = " + content);
        final String method = "smsNotification";
        if (NetService.getInstance().isDisable(method)) {
            TSnackbarUtil.TSBError(getActivity(), "请稍候...");
            return;
        }
        NetService.getInstance()
                .method(method)
                .smsNotification(content)
                .timeout(10, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponseEntity>() {
                    @Override
                    public void onSuccess(BaseResponseEntity responseEntity) {
                        Logger.d("onSuccess : " + responseEntity.toString());
                        TSnackbarUtil.TSBSuccess(getActivity(), "设置成功");
                    }

                    @Override
                    public String onOver() {
                        return method;
                    }
                });
    };

    private void getStates() {
        final String method = "getSmsNotification";
        if (NetService.getInstance().isDisable(method)) {
            TSnackbarUtil.TSBError(getActivity(), "请稍候...");
            return;
        }
        NetService.getInstance()
                .method(method)
                .getSmsNotification(String.valueOf(BasicApp.getInstance().getManager().getManagerId()), BasicApp.getInstance().getWatch().getMeid())
                .timeout(10, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<SmsNotification>() {
                    @Override
                    public void onSuccess(SmsNotification response) {
                        ArrayList<SmsNotification.Data> data = response.data;
                        for (int i = 0; i < data.size(); i++) {
                            int type = data.get(i).getType();
                            int status = data.get(i).getStatus();
                            if (type == 1){
                                mBinding.smsSos.setChecked(status == 1);
                            } else {
                                mBinding.smsBattery.setChecked(status == 1);
                            }
                        }
                    }

                    @Override
                    public String onOver() {
                        return method;
                    }
                });
    }

}
